// tbkPackage/pages/detail/detail.js
var common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        detail: {
            priceInfo: {
                price: 0
            },
            min_group_price: 0,
            coupon_discount: 0
        },
        currentSwiper: 0,
        toTop: 20,
        leader: 0,
        commissionname: "返利",
        noshare: true,
        iam_leader: false,
        commission: 0
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        //获取转发的团长信息
                if (options.leader) {
            this.setData({
                leader: options.leader
            });
        }
        var channel = options.channel;
        var id = options.id;
        let channelname = channel == "jingdong" ? "京东" : "拼多多";
        that.setData({
            channel: channel,
            id: id,
            channelname: channelname
        });
        that.getBase();
        that.getMyLeaderInfo();
        that.getProDetail(channel, id);
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        if (this.data.noshare == false) {
            this.setData({
                noshare: true
            });
        }
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        this.getProDetail(that.data.skuid);
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        let that = this;
        let leaderinfo = that.data.leader ? "&leader=" + that.data.leader : "";
        let commissionNotice = that.data.commissionUser ? "，下单还有返利哦" : "";
        let title = that.data.channel == "jingdong" ? that.data.detail.skuName + commissionNotice : that.data.detail.goods_name + commissionNotice;
        //当团长分享的时候 不显示团长佣金
                if (that.data.iam_leader) {
            this.setData({
                noshare: false
            });
        }
        return {
            title: title,
            path: "/tbkPackage/pages/detail/detail?channel=" + that.data.channel + "&id=" + that.data.id + leaderinfo
        };
    },
    //全局基础配置信息，这个模块暂时只用头部颜色，新版无头的也可能不用了
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {}
        });
    },
    //获取团长信息
    getMyLeaderInfo: function() {
        var that = this;
        var user = wx.getStorageSync("user");
        common.request({
            endpoint: "leader/myleaderinfo",
            method: "post",
            data: {
                openid: user.openid
            },
            success: res => {
                //判断哪个团长优先
                //1.我是团长，则优先用自己
                //2.本次分享团长 优先级 大于 历史绑定团长
                if (res.extra.iam_leader) {
                    var leader = res.extra.leader_id;
                } else {
                    var leader = that.data.leader != "" ? that.data.leader : res.extra.leader_id;
                }
                let commissionname = res.extra.iam_leader == 1 ? "返佣" : "返利";
                that.setData({
                    iam_leader: res.extra.iam_leader,
                    leader: leader,
                    commissionname: commissionname
                });
                //获取佣金比例
                                common.request({
                    endpoint: "Taoke/common/brokerage-setting-view",
                    method: "post",
                    data: {},
                    success: res => {
                        //根据身份，判断显示的佣金或返利比例
                        if (that.data.iam_leader) {
                            var commission = res.extra.is_taoke_leader_brokerage == 1 ? res.extra.taoke_leader_brokerage_rate : 0;
                        } else {
                            var commission = res.extra.is_taoke_user_brokerage == 1 ? res.extra.taoke_user_brokerage_rate : 0;
                        }
                        that.setData({
                            commission: commission,
                            commission_leader: res.extra.taoke_leader_brokerage_rate,
                            commission_user: res.extra.taoke_user_brokerage_rate,
                            commissionUser: res.extra.is_taoke_user_brokerage
                        });
                    }
                });
            }
        });
    },
    //获取商品详情
    getProDetail: function(channel, id) {
        let that = this;
        if (channel == "jingdong") {
            common.request({
                endpoint: "Taoke/" + channel + "/goods-detail",
                method: "post",
                data: {
                    skuIds: id
                },
                success: function(res) {
                    if (res.extra.code == 403) {
                        wx.showModal({
                            title: "提示",
                            content: res.extra.message,
                            showCancel: false,
                            success: function() {
                                wx.navigateBack({});
                            }
                        });
                    }
                    that.setData({
                        detail: res.extra.data[0]
                    });
                    wx.setNavigationBarTitle({
                        title: res.extra.data[0].goods_name
                    });
                    that.getUrl(channel, res.extra.data[0].materialUrl);
                }
            });
        } else {
            common.request({
                endpoint: "Taoke/" + channel + "/goods-detail",
                method: "post",
                data: {
                    goods_sign: id
                },
                success: function(res) {
                    that.setData({
                        detail: res.extra.goods_detail_response.goods_details[0]
                    });
                    wx.setNavigationBarTitle({
                        title: that.data.detail.goods_name
                    });
                    that.getUrl(channel, res.extra.goods_detail_response.goods_details[0].goods_id);
                }
            });
        }
    },
    //获取推广链接
    getUrl: function(channel, url) {
        let that = this;
        let uniacid_local = wx.getStorageSync("uniacid");
        var extData = wx.getStorageSync("user").uid + "," + that.data.leader + "," + uniacid_local;
        if (channel == "jingdong") {
            //请求京东推广链接
            common.request({
                endpoint: "Taoke/" + channel + "/promotion-common-get",
                method: "post",
                data: {
                    materialId: url,
                    ext1: extData
                },
                success: function(res) {
                    that.setData({
                        clickURL: res.extra.data.clickURL
                    });
                }
            });
        } else {
            //请求拼多多推广链接
            common.request({
                endpoint: "Taoke/" + channel + "/goods-promotion-url-generate",
                method: "post",
                data: {
                    goods_id: url,
                    custom_parameters: extData
                },
                success: function(res) {
                    if (res.extra.generate == 1) {
                        that.setData({
                            tuiGuangurl: res.extra.rp_promotion_url_generate_response.url_list[0]
                        });
                    } else {
                        that.setData({
                            tuiGuangurl: res.extra.goods_promotion_url_generate_response.goods_promotion_url_list[0]
                        });
                    }
                }
            });
        }
    },
    swiperChange: function(e) {
        this.setData({
            currentSwiper: e.detail.current
        });
    },
    navigateToMiniProgram() {
        let that = this;
        console.log("dgasdgasjdgasjdasda");
        if (that.data.channel == "jingdong") {
            wx.navigateToMiniProgram({
                appId: "wx13e41a437b8a1d2e",
                path: "/pages/jingfenblank/item?spreadUrl=" + encodeURIComponent(that.data.clickURL),
                envVersion: "trail",
                success(res) {
                    console.log("跳转京东小程序");
                }
            });
        } else {
            //跳转到拼多多
            wx.navigateToMiniProgram({
                appId: that.data.tuiGuangurl.we_app_info.app_id,
                path: that.data.tuiGuangurl.we_app_info.page_path,
                success(res) {
                    console.log("navigate success");
                }
            });
        }
    },
    //监听下拉，标题放上
    onPageScroll: function(e) {
        let top = e.scrollTop >= 200 ? 200 : e.scrollTop;
        this.setData({
            toTop: -top / 2 + 20
        });
    }
});